package com.jianbing.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianbing.entity.Result;
import com.jianbing.service.ShoppingService;
import com.jianbing.util.StringUtils;

import net.sf.json.JSONObject;

/**
 * 
 * @author cwl
 * @date 2017年6月12日
 *
 */

@Controller
@RequestMapping("/shop")
public class ShoppingController {

	Logger logger = Logger.getLogger(ShoppingController.class);

	@Autowired
	private ShoppingService shoppingService;

	/**
	 * 
	 * 合作方获取电商数据
	 * 
	 * @param request
	 * @param response
	 * @param body
	 * @return
	 */
	@RequestMapping(value = "queryShoppingData", method = RequestMethod.POST)
	@ResponseBody
	public Result queryShoppingData(HttpServletRequest request, HttpServletResponse response,
			@RequestBody String body) {
		Result result = new Result();
		logger.info("查询电商数据" + body);
		try {
			JSONObject json = JSONObject.fromObject(body);
			if (json.get("sign") == null || json.get("params") == null || json.get("type") == null) {
				result.setCode(101);
				result.setMsg("参数错误");
				return result;
			}
			return shoppingService.getShoppingData(result, json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
			return result;
		}
	}

	/**
	 * 
	 * 查询电商任务状态
	 * 
	 * @author cwl
	 * @date 2017年6月12日
	 *
	 * @param request
	 * @param response
	 * @param orderSn
	 * @param taskId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/queryShoppingTaskStatus", method = RequestMethod.GET)
	@ResponseBody
	public Result queryShoppingTaskStatus(HttpServletRequest request, HttpServletResponse response, String orderSn,
			Integer taskId, String status, String type) {
		Result result = new Result();
		if (StringUtils.isEmpty(type)) {
			result.setCode(-1);
			result.setMsg("请指定任务类型");
			return result;
		}
		ModelMap model = new ModelMap();
		model.addAttribute("list", shoppingService.queryShoppingTaskStatus(orderSn, taskId, status, type));
		result.setData(model);
		return result;
	}

	/**
	 * 
	 * 更新电商任务状态
	 * 
	 * @author cwl
	 * @date 2017年6月12日
	 *
	 * @param request
	 * @param response
	 * @param orderSn
	 * @param taskId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/updateShoppingTaskStatus", method = RequestMethod.POST)
	@ResponseBody
	public Result updateShoppingTaskStatus(HttpServletRequest request, HttpServletResponse response, String orderSn,
			Integer taskId, String status, String type) {
		Result result = new Result();
		if (StringUtils.isEmpty(orderSn) || StringUtils.isEmpty(status) || taskId == null
				|| StringUtils.isEmpty(type)) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}
		try {
			shoppingService.updateShoppingTaskStatus(orderSn, taskId, status, type);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
			return result;
		}
		return result;
	}
}
